<?
/**********Khoi tao bien*******************/
$sqlClass = new SqlClass ();
$errors = array ();
global $frm, $smarty, $group, $settings;


/************************function area****************************/

function notify_about_addition($row) {
	global $settings;
	$row ['withdrawal_type'] = ($row ['withdrawal_type'] == 1 ? 'Manual' : ($row ['withdrawal_type'] == 2 ? 'Instant' : 'Automatic'));
	send_mail ( 'listing_added', $settings ['admin_email'], $settings ['system_email'], $settings ['system_email_password'], $row );
}

/**
 * Lay groupId ma nguoi dung chon
 * tr/hop phi nguoi dung tra ($fee) nho hon phi cua $group_id hien tai => set lai $groupId dua vao fee nguoi dung chuyen vao
 * tr/hop phi $fee > phi cua group => tra $group_id 
 * @param $group_id groupId nguoi dung add
 * @param $fee phi nguoi dung tra
 * @param $premiumFee: phi premium listing 
 * @param $normal : phi normal listing
 * @param $trial : phi trail lisint
 * @return groupId dua vao fee nguoi dung tra va $group_id nguoi dung chon
 */
function getGroupId($group_id, $fee, $premiumFee, $normalFee, $trialFee) {
	$groupIdRtn = "";
	$premiumGroupId = GlobalConst::$GROUP_PREMIUM_ID;
	$normalGroupId = GlobalConst::$GROUP_NORMAL_ID;
	$trialGroupId = GlobalConst::$GROUP_TRIAL_ID;
	if ($group_id == $premiumGroupId && $fee >= $premiumFee) {
		//tr/h nguoi dung premium listing
		$groupIdRtn = $premiumGroupId;
	} else if ($group_id == $normalGroupId && $fee >= $normalFee) {
		//tr/h nguoi dung normal listing
		$groupIdRtn = $normalGroupId;
	} else if ($group_id == $trialGroupId && $fee >= $trialFee) {
		//tr/h nguoi dung trial listing
		$groupIdRtn = $trialGroupId;
	}
	return $groupIdRtn;
}

/**
 * Dieu khien hien thi trang add_program
 * @param unknown_type $groupId
 * @param unknown_type $batch
 * @param unknown_type $paymentType
 * @param unknown_type $emailContact
 * @param unknown_type $errors
 */
function displayAddProgram2Page($groupId, $batch, $paymentType, $emailContact, $errors) {
	global $smarty;
	global $sqlClass;
	$smarty->assign ( 'group_id', $groupId );
	$smarty->assign ( 'group_name', $sqlClass->getGroupNameById ( $groupId ) );
	$smarty->assign ( 'batch', $batch );
	$smarty->assign ( 'payment_type', $paymentType );
	$smarty->assign ( 'email', $emailContact );
	$smarty->assign ( 'payments', CommonClass::getPayments () );
	$smarty->assign ( 'errors', $errors );
	$smarty->display ( 'add_program2.tpl' );
}

/**
 * lay do dai cua 1 chuoi sau khi cat bo khoang trang 2 ben
 * @param  $str: chuoi can lay do dai
 * @return do dai cua chuoi
 */
function getStrLen($str) {
	return strlen ( trim ( $str ) );
}


$smarty->display ('add_program1.tpl');

/*********************************************************************************************/
//if ($frm ['display'] == 'added') {
//	$smarty->assign ( 'lid', $frm ['lid'] );
//	$smarty->assign ( 'fee', $frm ['fee'] );
//	$smarty->assign ( 'ip', $_SERVER ['REMOTE_ADDR'] );
//	$smarty->assign ( 'email', $frm ['email'] );
//	$smarty->display ( 'listing_added.tpl' );
//	exit ();
//}
//da chuyen tien vao tai khoan
//if ($frm ['action'] == 'add_money' 
//	&& $frm ['payment_type'] == GlobalConst::$PAYMENT_TYPE_LIBERTY_RESERVER) {
//
//	$say = $frm['say'];
//	if('success' != say){
//		$smarty->assign ( 'say', 'notsuccess' );
//		$smarty->display ( 'lr_processing.tpl' );
//		exit();
//	}
//	
//	$paidto = quote ( $frm ['lr_paidto'] );
//	$paidby = quote ( $frm ['lr_paidby'] );
//	$lr_amnt = quote ( $frm ['lr_amnt'] );
//	$lr_currency = quote ( $frm ['lr_currency'] );
//	$lr_transfer = quote ( $frm ['lr_transfer'] );
//	$lr_store = quote ( $frm ['lr_store'] );
//	$lr_encrypted = quote ( $frm ['lr_encrypted'] );
//	$action_type = quote ( $frm ['action_type'] );
//	$payment_type = quote ( $frm ['payment_type'] );
//	
//	$hash_recieved = $paidto . ':' . $paidby . ':' . stripslashes ( $lr_store ) . ':' . $lr_amnt . ':' . $lr_transfer . ':' . $lr_currency . ':' . $settings ['second_password'];
//	
//	$hash_encrypt = hash ( 'sha256', $hash_recieved );
//	//print $hash_encrypt;
//	//if de check dau vao dung, neu nguoi lai chuyen den trang error
//	if (isset ( $paidto ) && $paidto == $settings ['admin_lr_account'] 
//		&& isset ( $paidby ) && getStrLen ( $paidby ) > 0 
//		&& isset ( $lr_store ) && stripslashes ( $lr_store ) == $settings ['libertyreserve_store'] 
//		&& isset ( $lr_amnt ) && getStrLen ( $lr_amnt ) > 0 
//		&& isset ( $lr_transfer ) && getStrLen ( $lr_transfer ) > 0 
//		&& isset ( $lr_currency ) && getStrLen ( $lr_currency ) > 0 
//		&& isset ( $lr_encrypted ) && $lr_encrypted == $hash_encrypt
//		) {
//		
//		//data transfered from lr is ok
//		//check exist in history
//		$sqlStr = "select * from hl_payments_history where payment_type = '" . quote ( $frm ['payment_type'] ) . "'  and batch ='" . $lr_transfer . "'";
//		$result = $sqlClass->executeSql ( $sqlStr );
//		if(!$result){
//			header ( "Location: ?a=error" );
//			exit ();
//		}
//		//truong da ton batch
//		if (mysql_num_rows ( $result ) > 0) {
//			header ( "Location: ?a=error" );
//			exit ();
//		}
//		//add listing
//		if ($action_type == GlobalConst::$ACTION_ADD_LISTING) {
//			//get fee listing of group
//			$premiumFee = $settings ['premiummonitorfees'] + $settings ['premiumlistingfees'];
//			$normal = $settings ['normalmonitorfees'] + $settings ['normallistingfees'];
//			$trial = $settings ['trialmonitorfees'] + $settings ['triallistingfees'];
//			$group_id = quote ( $frm ['group_id'] );
//			$group_id = getGroupId ( $group_id, $lr_amnt, $premiumFee, $normal, $trial );
//			//truong khong tim duoc $groupd_id hoac $groupd_id khong duoc khoi tao
//			if (! isset ( $group_id ) || strlen ( trim ( $group_id ) ) == 0) {
//				header ( "Location: ?a=error" );
//				exit ();
//			}
//			//insert du lieu vao bao hl_payments_history
//			
//
//			$inserts = array ();
//			array_push ( $inserts, ' action_type = \'' . GlobalConst::$ACTION_ADD_LISTING . '\'' );
//			array_push ( $inserts, ' group_id = \'' . $group_id . '\'' );
//			array_push ( $inserts, ' payment_type = \'' . GlobalConst::$PAYMENT_TYPE_LIBERTY_RESERVER . '\'' );
//			array_push ( $inserts, ' batch = \'' . $lr_transfer . '\'' );
//			array_push ( $inserts, ' email_contact = \'' . $frm ['email'] . '\'' );
//			array_push ( $inserts, ' paid_to = \'' . $paidto . '\'' );
//			array_push ( $inserts, ' pair_by = \'' . $paidby . '\'' );
//			array_push ( $inserts, ' payment_fee = \'' . $lr_amnt . '\'' );
//			array_push ( $inserts, ' action_status = \'' . GlobalConst::$STATUS_WAITING . '\'' );
//			array_push ( $inserts, ' date_add = \'' . date ( "Y-m-d" ) . '\'' );
//			
//			$q = 'insert into '.Tables::$HL_PAYMENTS_HISTORY .' set ' . join ( ',', $inserts );
//			$sth = $sqlClass->executeSql ( $q );
//			if (! $sth) {
//				header ( "Location: ?a=error" );
//				exit ();
//			}
//			
//			displayAddProgram2Page($group_id, $lr_transfer, GlobalConst::$PAYMENT_TYPE_LIBERTY_RESERVER, $frm ['email']);
//			exit ();
//		
//		} else if (GlobalConst::$ACTION_UPGRADE_LISTING == $action_type) {
//			//upgrade listing
//		} else if (GlobalConst::$ACTION_ADD_ADVERTISEMENT == $action_type) {
//			//ADD ADVERTISEMENT
//		} else if (GlobalConst::$ACTION_UPGRADE_ADVERTISEMENT == $action_type) {
//			//UPGRADE ADVERTISEMENT
//		} else {
//			//xay ra khi nguoi dung chon $action_type khong phai la addlisting, upgrade listing, add advertisements, upgrade advertisements
//			header ( "Location: ?a=error" );
//			exit ();
//		}
//	} else {
//		header ( "Location: ?a=error" );
//		exit ();
//	}
//}else if($frm ['action'] == 'verify'
//	&& $frm ['payment_type'] == GlobalConst::$PAYMENT_TYPE_LIBERTY_RESERVER ){
//	//truong hop nguoi dung da pay money , verify batch no nguoi dung nhap vao
//	$batch = quote($frm['batch']);
//	//truong hop batch no bang rong	
//	if (CommonClass::nullOrBlank ( $batch )) {
//		array_push ( $errors, array ('name' => 'Please enter your batch no!' ) );
//		$smarty->assign ( 'errors', $errors );
//		$smarty->display ( 'add_program1.tpl' );
//		exit ();
//	}
//	$sqlStr = 'select * from '. Tables::$HL_PAYMENTS_HISTORY 
//				. ' where batch = \''. $batch .'\' 
//						and payment_type = \''. GlobalConst::$PAYMENT_TYPE_LIBERTY_RESERVER .'\' 
//						and action_type = \''. GlobalConst::$ACTION_ADD_LISTING .'\'
//						and action_status = 0 ';
//	$result = $sqlClass->executeSql ( $sqlStr );
//	if (! $result) {
//		header ( "Location: ?a=error" );
//		exit ();
//	}
//	$rowNum = mysql_num_rows ( $result );
//	if($rowNum < 1){
//		array_push ( $errors, array ('name' => 'Batch No. Not Found.You should pay to get one.' ) );
//		$smarty->assign ( 'errors', $errors );
//		$smarty->display ( 'add_program1.tpl' );
//		exit ();
//	}
//	$row = mysql_fetch_array($result);
//	displayAddProgram2Page($row['group_id'], $row['batch'], $row['payment_type'], $row['email_contact']);
//	exit ();
//}else if ($frm ['action'] == 'confirm') {
//	$success = 0;
//	if ($frm ['code']) {
//		$q = 'select * from hl_listings where confirm = \'' . quote ( $frm ['code'] ) . '\' limit 0, 1';
//		$sth = mysql_query ( $q );
//		$row = mysql_fetch_array ( $sth );
//		if ($row ['id']) {
//			$q = 'update hl_listings set confirm = \'0\' where confirm = \'' . quote ( $frm ['code'] ) . '\'';
//			$sth = mysql_query ( $q );
//			$success = 1;
//			if ($settings ['notify_about_addition']) {
//				$q = 'select * from hl_groups where id = ' . $row ['group_id'];
//				($sth = mysql_query ( $q ) or print mysql_error ());
//				$group = mysql_fetch_array ( $sth );
//				$row ['group'] = $group ['name'];
//				notify_about_addition ( $row );
//			}
//		}
//	}
//	
//	$smarty->assign ( 'success', $success );
//	$smarty->display ( 'listing_confirmed.tpl' );
//	exit ();
//}



//$groups = array ();
//$frm['type'] = intval ($frm['type']);
//$q = 'select * from hl_groups where status = 1 and reg_enabled = 1 and id = ' . $frm['group_id'];
//($sth = mysql_query ($q) OR print mysql_error ());
//$group = mysql_fetch_array ($sth);
//if (!$group){
//	header ('Location: ?a=arvertise');
//	exit ();
//}
//$smarty->assign ('group', $group);
//$payments = preg_split ('/\\s*,\\s*/', $settings['payments']);







//$paysystems = array ();
//$selected = $frm['payments'];
//if (!$selected){
//	$selected = array ();
//}
//foreach ($payments as $payment){
//	array_push ($paysystems, array ('name' => $payment, 'selected' => in_array ($payment, array_keys ($selected))));
//}



//$smarty->assign ('payments', $paysystems);
//$smarty->assign ('errors', $errors);
////$smarty->display ('add_' . strtolower ($group['type']) . '.tpl');
//$smarty->display ('add_program2.tpl');
?>
